<!--
 * @Description: 我的订单页面组件
 -->
<template>
  <div class="order">
    <!-- 我的订单头部 -->
    <div class="order-header">
      <div class="order-header-content">
        <p>
          <em class="el-icon-s-order" style="font-size: 30px;color: #ff6700;"></em>
          我的订单
        </p>
      </div>
    </div>
    <!-- 我的订单头部END -->

    <!-- 我的订单主要内容 -->
    <div class="order-content" v-if="orders.length>0">
      <div class="content" v-for="(item,index) in orders" :key="index">
        <ul>
          <!-- 我的订单表头 -->
          <li class="order-info">
            <div class="order-id">订单编号: {{item.order_id}}</div>
            
            <div class="order-status"><span style="margin: 10px;">订单状态:</span> 
              <el-tag v-if="item.order_status == 0" type="info">待付款</el-tag>
              <el-tag v-if="item.order_status == 1" type="warning">等待发货</el-tag>
              <!-- <el-tag v-if="item.order_status == 2">已发货</el-tag> -->
              <el-button v-if="item.order_status == 2" size="mini" 
                type="warning" @click="updateOrder(item,index)">确认收货</el-button>
              <el-tag v-if="item.order_status == 3" type="success">订单已完成</el-tag>
            </div>
            <div class="order-time">订单时间: {{item.order_date}}</div>
          </li>
          <li class="header">
            <div class="pro-img"></div>
            <div class="pro-name">商品名称</div>
            <div class="pro-price">单价</div>
            <div class="pro-num">数量</div>
            <div class="pro-total">小计</div>
            <div class="pro-evaluate">评价</div>
          </li>
          <!-- 我的订单表头END -->

          <!-- 订单列表 -->
          <li class="product-list" v-for="(product,i) in item.order_itemList" :key="i">
            <div class="pro-img">
              <router-link :to="{ path: '/goods/details', query: {productID:product.goods_id} }">
                <img :src="$target + product.goods.good_picture" alt=""/>
              </router-link>
            </div>
            <div class="pro-name">
              <router-link
                :to="{ path: '/goods/details', query: {productID:product.goods_id} }"
              >{{product.goods.good_name}}</router-link>
            </div>
            <div class="pro-price">{{product.discounted_price}}元</div>
            <div class="pro-num">{{product.item_count}}</div>
            <div class="pro-total pro-total-in">{{product.discounted_price*product.item_count}}元</div>
            <div  class="pro-evaluate">
              <el-button v-if="item.order_status == 3" type="primary" icon="el-icon-edit" circle @click="openAdd(product)"></el-button>
              <span v-else>订单未完成</span>
            </div>
          </li>
        </ul>
        <div class="order-bar">
          <div class="order-bar-left">
            <span class="order-total">
              共
              <span class="order-total-num">{{total[index].totalNum}}</span> 件商品
            </span>
          </div>
          <div class="order-bar-right">
            <span>
              <span class="total-price-title">合计：</span>
              <span class="total-price">{{total[index].totalPrice}}元</span>
            </span>
          </div>
          <!-- 订单列表END -->
        </div>
      </div>
      <div style="margin-top:-40px;"></div>
    </div>
    <!-- 我的订单主要内容END -->

    <!-- 订单为空的时候显示的内容 -->
    <div v-else class="order-empty">
      <div class="empty">
        <h2>您的订单还是空的！</h2>
        <p>快去购物吧！</p>
      </div>
    </div>
    <!-- 订单为空的时候显示的内容END -->

    <!-- 添加评论 -->
    <add :addEv="addEv" @evaluateClose="closeAdd"></add>
  </div>
</template>
<script>
import { findOrderByUserId, updateOrderStatus } from "../api/order"
import add from "./evaluate/add"
export default {
  components: { // 组件的引用
      add
  },
  data() {
    return {
      orders: [], // 订单列表
      total: [], // 每个订单的商品数量及总价列表
      addEv: {
        visible: false,
        good_id: 0,
        u_id: 0,
      }
    }
  },
  activated() {
    // 获取订单数据
    var data = {
      order_user_id: this.$store.getters.getUser.id,
      pageNum: 1,
      pageSize: 3
    }
    findOrderByUserId(data).then((res) => {
      console.log(res)
      this.orders = res.order_tableList
    })
  },
  watch: {
    // 通过订单信息，计算出每个订单的商品数量及总价
    orders: function(val) {
      let total = [];
      for (let i = 0; i < val.length; i++) {
        const element = val[i].order_itemList;

        let totalNum = 0;
        let totalPrice = val[i].order_sum_price;
        for (let j = 0; j < element.length; j++) {
          const temp = element[j];
          totalNum += temp.item_count;
        }
        total.push({ totalNum, totalPrice });
      }
      this.total = total;
    }
  },
  methods: {
    openAdd(product) {
      this.addEv.good_id = product.goods_id
      this.addEv.u_id = this.$store.getters.getUser.id
      this.addEv.visible = !this.addEv.visible
    },
    closeAdd() {
      this.addEv.visible = !this.addEv.visible
    },
    /**
     * 修改订单状态（确认收货）
     */
    updateOrder(item,index) {
      this.$confirm('是否确认收货?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        /**
         * 修改订单状态
         */
        var data = {
          order_id: item.order_id,
          order_status: item.order_status + 1
        }
        // console.log("订单修改请求参数" + data)
        updateOrderStatus(data)
          .then((res) => {
            // console.log(res.message)
            if (res.message == '订单状态修改成功') {
                this.$message.success('确认收货完成');
                this.orders[index].order_status += 1
            } else {
                this.$message.error('确认收货失败');
            }
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消确认收货'
        });          
      });

      
    },
  }
};
</script>
<style scoped>
.order {
  background-color: #f5f5f5;
  padding-bottom: 20px;
}
/* 我的订单头部CSS */
.order .order-header {
  height: 64px;
  border-bottom: 2px solid #ff6700;
  background-color: #fff;
  margin-bottom: 20px;
}
.order .order-header .order-header-content {
  width: 1225px;
  margin: 0 auto;
}
.order .order-header p {
  font-size: 28px;
  line-height: 58px;
  float: left;
  font-weight: normal;
  color: #424242;
}
/* 我的订单头部CSS END */
.order .content {
  width: 1225px;
  margin: 0 auto;
  background-color: #fff;
  margin-bottom: 50px;
}

.order .content ul {
  background-color: #fff;
  color: #424242;
  line-height: 85px;
}
/* 我的订单表头CSS */
.order .content ul .order-info {
  height: 60px;
  line-height: 60px;
  padding: 0 26px;
  color: #424242;
  border-bottom: 1px solid #ff6700;
}
.order .content ul .order-info .order-id {
  float: left;
  color: #ff6700;
}
.order .content ul .order-info .order-time {
  float: left;
  padding-left: 220px;
}
.order .content ul .order-info .order-status {
  width: 180px;
  text-align: left;
  float: right;
}

.order .content ul .header {
  height: 85px;
  padding-right: 26px;
  color: #424242;
}
/* 我的订单表头CSS END */

/* 订单列表CSS */
.order .content ul .product-list {
  height: 85px;
  padding: 15px 26px 15px 0;
  border-top: 1px solid #e0e0e0;
}
.order .content ul .pro-img {
  float: left;
  height: 85px;
  width: 120px;
  padding-left: 40px;
}
.order .content ul .pro-img img {
  height: 80px;
  width: 80px;
}
.order .content ul .pro-name {
  float: left;
  width: 300px;
}
.order .content ul .pro-name a {
  color: #424242;
}
.order .content ul .pro-name a:hover {
  color: #ff6700;
}
.order .content ul .pro-price {
  float: left;
  width: 160px;
  padding-right: 18px;
  text-align: center;
}
.order .content ul .pro-num {
  float: left;
  width: 190px;
  text-align: center;
}
.order .content ul .pro-total {
  float: left;
  width: 160px;
  /* padding-right: 0px; */
  padding-left: 90px;
  text-align: left;
}
.order .content ul .pro-total-in {
  color: #ff6700;
}
.order .content ul .pro-evaluate {
  float: left;
  width: 100px;
  text-align: center;
}


.order .order-bar {
  width: 1185px;
  padding: 0 20px;
  border-top: 1px solid #ff6700;
  height: 50px;
  line-height: 50px;
  background-color: #fff;
}
.order .order-bar .order-bar-left {
  float: left;
}
.order .order-bar .order-bar-left .order-total {
  color: #757575;
}
.order .order-bar .order-bar-left .order-total-num {
  color: #ff6700;
}
.order .order-bar .order-bar-right {
  float: right;
}
.order .order-bar .order-bar-right .total-price-title {
  color: #ff6700;
  font-size: 14px;
}
.order .order-bar .order-bar-right .total-price {
  color: #ff6700;
  font-size: 30px;
}
/* 订单列表CSS END */

/* 订单为空的时候显示的内容CSS */
.order .order-empty {
  width: 1225px;
  margin: 0 auto;
}
.order .order-empty .empty {
  height: 300px;
  padding: 0 0 130px 558px;
  margin: 65px 0 0;
  background: url(../assets/imgs/cart-empty.png) no-repeat 124px 0;
  color: #b0b0b0;
  overflow: hidden;
}
.order .order-empty .empty h2 {
  margin: 70px 0 15px;
  font-size: 36px;
}
.order .order-empty .empty p {
  margin: 0 0 20px;
  font-size: 20px;
}
/* 订单为空的时候显示的内容CSS END */
</style>